package com.flowershop.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private FileUploadConfig fileUploadConfig;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String uploadPath = fileUploadConfig.getPath();
        // 确保路径以 / 结尾
        if (!uploadPath.endsWith("/")) {
            uploadPath = uploadPath + "/";
        }
        // 使用 file: 协议，并确保路径格式正确
        String location = "file:" + uploadPath;
        if (location.startsWith("file://")) {
            location = "file:/" + location.substring(7);
        }
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations(location);
    }
} 